package test;

import java.io.*;
import java.util.*;

/**
 * @author oldliu
 * @since 1.0
 */
public class Caipiao {
    public static void main(String[] args) {
        try {
            //{02:0,01:2}  (key-value)
            //数组，int []redball=new int[33];
            Map<Integer, Integer> red = new HashMap<>();
            Map<Integer, Integer> blue = new HashMap<>();
            BufferedReader reader = new BufferedReader(new FileReader("d:/Documents/桌面/82/lottery2.txt"));
            String s = null;
            while ((s = reader.readLine()) != null) {
               // System.out.println(s);
                //找到所有红球
                String m = s.substring(s.indexOf(":") + 1, s.indexOf("+"));
                String[] reda = m.split(" ");
                for (String t : reda) {
                   int i=Integer.parseInt(t);//转换成整数
                   //找不到，返回第二个参数作为默认值
                   red.put(i,red.getOrDefault(i,0)+1);
                }

//                for (int i = 0; i < m.length(); i += 2) {
//                    int s1 = Integer.parseInt(m.substring(i, i + 2));
//                    if (red.containsKey(s1)) {
//                        red.put(s1, red.get(s1) + 1);
//                    } else {
//                        red.put(s1, 0);
//                    }
//                }
                //找到所有篮球
                String m2 = s.substring(s.indexOf("+") + 1);//为什么加1？
                int s2 = Integer.parseInt(m2);
                blue.put(s2,blue.getOrDefault(s2,0)+1);
            }
            System.out.println(red);
            Integer maxRed = Collections.max(red.values());
            Set<Map.Entry<Integer, Integer>> entries = red.entrySet();
            System.out.println("最多次数的红球是：");
            for(Map.Entry<Integer, Integer> o: entries) {
                if (o.getValue() == maxRed) {
                    System.out.print(o.getKey());
                    System.out.print('\t');
                }
            }
            System.out.println(blue);
            reader.close();
            List<Map.Entry<Integer,Integer>> redlist=new ArrayList<>();
            redlist.addAll(red.entrySet());
            redlist.sort(new M());
            //Collections.sort(redlist,new M());
            System.out.println("排序后：");
            System.out.println(redlist);
            //Map.Entry<Integer, Integer> integerIntegerEntry = redlist.get(0);
            //System.out.println(integerIntegerEntry.getClass().getName());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
//比较器
class M implements Comparator<Map.Entry<Integer,Integer>>{

    @Override
    public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
        return o2.getValue()-o1.getValue();
    }
}